<?php

namespace app\adminapi\logic\room\department;

use app\common\enum\YesNoEnum;
use app\common\logic\BaseLogic;
use app\common\model\room\department\RoomDepartment;
use app\common\model\room\department\RoomDepartmentDevice;
use app\common\service\ConfigService;
use think\facade\Db;


/**
 * RoomDepartment逻辑
 * Class RoomDepartmentLogic
 * @package app\adminapi\logic
 */
class RoomDepartmentLogic extends BaseLogic {


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/23 10:30
     */
    public static function add(array $params): bool {
        Db::startTrans();
        try {
            $rid = RoomDepartment::create([
                'cid' => $params['cid'],
                'bid' => $params['bid'],
                'department_id' => $params['department_id'] ?? '',
                'department' => !empty($params['department']) ? json_encode($params['department']) : null,
                'name' => $params['name'] ?? '',
                'status' => $params['status'] ?? YesNoEnum::YES,
                'telephone' => $params['telephone'] ?? '',
                'address' => $params['address'] ?? '',
                'sort' => $params['sort'] ?? 10000,
                'code' => $params['code'],
            ])->getLastInsID();

//            $sdc = ConfigService::get('room_apartment', 's_device_coefficient');
//            $sdp = ConfigService::get('room_apartment', 's_device_price');
            $ddc = ConfigService::get('room_apartment', 'd_device_coefficient');
            $ddp = ConfigService::get('room_apartment', 'd_device_price');

            RoomDepartmentDevice::create([
                "cid" => $params['cid'],
                "bid" => $params['bid'],
                "rid" => $rid,
                "room_master" => 1,
                "sort" => $params['sort'] ?? 10000,
                "create_time" => date("Y-m-d H:i:s"),
                "update_time" => date("Y-m-d H:i:s"),
                "device_type" => 2, // 默认电表
                "coefficient" => $ddc, // 系数
                "price" => $ddp, // 水电价
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/23 10:30
     */
    public static function edit(array $params): bool {
        Db::startTrans();
        try {
            RoomDepartment::where('id', $params['id'])->update([
                'cid' => $params['cid'],
                'bid' => $params['bid'],
                'department_id' => $params['department_id'] ?? '',
                'department' => !empty($params['department']) ? json_encode($params['department']) : null,
                'name' => $params['name'] ?? '',
                'status' => $params['status'] ?? YesNoEnum::YES,
                'telephone' => $params['telephone'] ?? '',
                'address' => $params['address'] ?? '',
                'sort' => $params['sort'] ?? 9999,
                'code' => $params['code'],
            ]);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/23 10:30
     */
    public static function delete(array $params): bool {
        RoomDepartmentDevice::whereIn("rid", $params['id'])->delete();
        return RoomDepartment::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/02/23 10:30
     */
    public static function detail($params): array {
        return RoomDepartment::findOrEmpty($params['id'])->toArray();
    }


    public static function updateStatus(array $params) {
        RoomDepartment::update([
            'id' => $params['id'],
            'status' => $params['status']
        ]);
        return true;
    }

}
